/*
 * NotasAdminView.java
 *
 * Created on 25 de diciembre de 2007, 2:54
 */
package jschoolmanagerportable.gui;

import javax.swing.table.TableColumn;
import jschoolmanagerportable.gui.model.NotaEditor;
import jschoolmanagerportable.gui.model.NotaRender;
import jschoolmanagerportable.gui.model.NotasAdminModelTable;
import jschoolmanagerportable.model.Asignatura;
import jschoolmanagerportable.model.Colegio;
import jschoolmanagerportable.model.Curso;

/**
 *
 * @author  Administrador
 */
public class NotasAdminView extends javax.swing.JInternalFrame {

    /** Creates new form NotasAdminView */
    public NotasAdminView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = javax.persistence.Persistence.createEntityManagerFactory("JSchoolManagerPortablePU").createEntityManager();
        colegioQuery = entityManager.createQuery("SELECT c FROM Colegio c");
        colegioList = org.jdesktop.observablecollections.ObservableCollections.observableList(colegioQuery.getResultList());
        cursoQuery = entityManager.createQuery("SELECT c FROM Curso c");
        cursoList = org.jdesktop.observablecollections.ObservableCollections.observableList(cursoQuery.getResultList());
        asignaturaQuery = entityManager.createQuery("SELECT a FROM Asignatura a");
        asignaturaList = org.jdesktop.observablecollections.ObservableCollections.observableList(asignaturaQuery.getResultList());
        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        colegiosCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cursosCombo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        asignaturasCombo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaNotas = new javax.swing.JTable();
        cargar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        porAlumno = new javax.swing.JRadioButton();
        porNota = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        periodosCombo = new javax.swing.JComboBox();

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jschoolmanagerportable.JSchoolManagerPortableApp.class).getContext().getResourceMap(NotasAdminView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        colegiosCombo.setName("colegiosCombo"); // NOI18N

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, colegioList, colegiosCombo);
        bindingGroup.addBinding(jComboBoxBinding);

        colegiosCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colegiosComboActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        cursosCombo.setName("cursosCombo"); // NOI18N

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cursoList, cursosCombo);
        bindingGroup.addBinding(jComboBoxBinding);

        cursosCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cursosComboActionPerformed(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        asignaturasCombo.setName("asignaturasCombo"); // NOI18N

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, asignaturaList, asignaturasCombo);
        bindingGroup.addBinding(jComboBoxBinding);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaNotas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tablaNotas.setName("tablaNotas"); // NOI18N
        tablaNotas.setRowSelectionAllowed(false);
        tablaNotas.setSurrendersFocusOnKeystroke(true);
        jScrollPane1.setViewportView(tablaNotas);

        cargar.setText(resourceMap.getString("cargar.text")); // NOI18N
        cargar.setName("cargar"); // NOI18N
        cargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cargarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        buttonGroup1.add(porAlumno);
        porAlumno.setSelected(true);
        porAlumno.setText(resourceMap.getString("porAlumno.text")); // NOI18N
        porAlumno.setName("porAlumno"); // NOI18N

        buttonGroup1.add(porNota);
        porNota.setText(resourceMap.getString("porNota.text")); // NOI18N
        porNota.setName("porNota"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(porAlumno)
                    .addComponent(porNota))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(porAlumno)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(porNota))
        );

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        periodosCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        periodosCombo.setName("periodosCombo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(asignaturasCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(colegiosCombo, 0, 268, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cursosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(periodosCombo, 0, 184, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cargar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(colegiosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(cursosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(asignaturasCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(periodosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(cargar, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void colegiosComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colegiosComboActionPerformed

        Colegio colegio = (Colegio) colegiosCombo.getSelectedItem();
        if (colegio == null) {
            return;
        }
        cursoList.clear();
        cursoList.addAll(colegio.getCursoCollection());
        periodosCombo.removeAllItems();
        if (colegio.getPeriodos() == 3) {
            periodosCombo.addItem("Primer Trimestre");
            periodosCombo.addItem("Segundo Trimestre");
            periodosCombo.addItem("Tercer Trimestre");
        }else{
            periodosCombo.addItem("Primer Semestre");
            periodosCombo.addItem("Segundo Semestre");            
        }
        
}//GEN-LAST:event_colegiosComboActionPerformed

    private void cursosComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cursosComboActionPerformed

        Curso curso = (Curso) cursosCombo.getSelectedItem();
        if (curso == null) {
            return;
        }
        asignaturaList.clear();
        asignaturaList.addAll(curso.getAsignaturaCollection());
    }//GEN-LAST:event_cursosComboActionPerformed

    private void cargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargarActionPerformed

        int periodo;
        String strPeriodo = (String) periodosCombo.getSelectedItem();
        if(strPeriodo.indexOf("Primer")==0){
            periodo=1;
        }else if(strPeriodo.indexOf("Segundo")==0){
            periodo=2;
        }else{
            periodo=3;
        }
        
        Asignatura asignatura = (Asignatura) asignaturasCombo.getSelectedItem();
        Curso curso = (Curso) cursosCombo.getSelectedItem();
        dataModel = new NotasAdminModelTable(curso.getAlumnoCollection(), asignatura, entityManager,periodo);
        tablaNotas.setModel(dataModel);

        NotaRender nr = new NotaRender();
        TableColumn column = null;
        for (int i = 0; i < tablaNotas.getColumnModel().getColumnCount(); i++) {
            column = tablaNotas.getColumnModel().getColumn(i);
            if (i == 0) {
                column.setPreferredWidth(300);
                column.setMaxWidth(300);
                column.setMinWidth(300);
            } else {
                column.setPreferredWidth(30);
                column.setMaxWidth(30);
                column.setMinWidth(30);
                column.setCellRenderer(nr);
            }
        }
        tablaNotas.setDefaultEditor(Double.class, new NotaEditor(this, 0.0, 7.0));        
}//GEN-LAST:event_cargarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<jschoolmanagerportable.model.Asignatura> asignaturaList;
    private javax.persistence.Query asignaturaQuery;
    private javax.swing.JComboBox asignaturasCombo;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cargar;
    private java.util.List<jschoolmanagerportable.model.Colegio> colegioList;
    private javax.persistence.Query colegioQuery;
    private javax.swing.JComboBox colegiosCombo;
    private java.util.List<jschoolmanagerportable.model.Curso> cursoList;
    private javax.persistence.Query cursoQuery;
    private javax.swing.JComboBox cursosCombo;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox periodosCombo;
    private javax.swing.JRadioButton porAlumno;
    private javax.swing.JRadioButton porNota;
    private javax.swing.JTable tablaNotas;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private NotasAdminModelTable dataModel;

    public void notaSiguiente(int column, int row, int cantidad) {
        int row_ori = row, col_ori = column;
        if (porNota.isSelected()) {
            column += cantidad;
        } else {
            if (row == (dataModel.getRowCount() - 1)) {
                row = 0;
            } else {
                row += cantidad;
            }
        }
        tablaNotas.requestFocus();
        tablaNotas.changeSelection(row, column, true, true);

        dataModel.actualizaPromedio(row_ori);
    }
}
